package com.qf.student.bo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.student.common.Const;
import com.qf.student.dao.AdminMapper;
import com.qf.student.dao.StudentMapper;
import com.qf.student.dao.TeacherMapper;
import com.qf.student.entity.Admin;
import com.qf.student.entity.Student;
import com.qf.student.entity.Teacher;
import com.qf.student.vo.input.LoginInputVo;
import com.qf.student.vo.output.LoginOutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author YaoJiXing
 * created 2022/11/21 21:15
 */
@Service
public class LoginBO {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    public LoginOutputVo getUserByUserName(LoginInputVo inputVo) {
        LoginOutputVo outputVo = null;

        if (inputVo.getType() == Const.UserType.STUDENT) {
            Student student = studentMapper.selectOne(
                    new QueryWrapper<Student>().eq("student_number", inputVo.getUsername()));
            if (student != null) {
                outputVo = new LoginOutputVo(student.getStudentId(),
                        student.getStudentName(),
                        student.getStudentPassword(),
                        Const.UserType.STUDENT);
            }
        } else if (inputVo.getType() == Const.UserType.TEACHER) {
            Teacher teacher = teacherMapper.selectOne(
                    new QueryWrapper<Teacher>().eq("teacher_number", inputVo.getUsername()));
            if (teacher != null) {
                outputVo = new LoginOutputVo(teacher.getTeacherId(),
                        teacher.getTeacherName(),
                        teacher.getTeacherPassword(),
                        Const.UserType.TEACHER);
            }
        } else if (inputVo.getType() == Const.UserType.ADMIN) {
            Admin admin = adminMapper.selectOne(
                    new QueryWrapper<Admin>().eq("admin_username", inputVo.getUsername()));
            if (admin != null) {
                outputVo = new LoginOutputVo(admin.getAdminId(),
                        admin.getAdminUsername(),
                        admin.getAdminPassword(),
                        Const.UserType.ADMIN);
            }
        }
        return outputVo;
    }

}
